/**
 * Licensed under The MIT License
 *  Copyright (c) 2011 Han Wen
 *  
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *  
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 **/

package edu.rochester.URshuttleschedule;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class DetailedSchedule extends Activity {
	protected int column;
	private TextView headTitle;
	protected ArrayList<String> detailschedule;
	private ArrayList<Integer> timeRow;
	private String[] stations;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.detaillist);
		ListView l1 = (ListView) findViewById(R.id.DListView01);

		Bundle bundle = this.getIntent().getExtras();
		column = bundle.getInt("col");
		stations = bundle.getStringArray("schedule"); // stations here is a
														// temp. var.
		int numItems = stations.length;
		detailschedule = new ArrayList<String>(numItems);
		timeRow = new ArrayList<Integer>(numItems);
		// get items at correct column
		for (int k = 0; k < numItems; k++) {
			int pos = -1;
			for (int l = 0; l < column; l++) {
				pos = stations[k].indexOf(',', pos + 1);
			}
			String sTmp = stations[k].substring(pos + 1,
					stations[k].indexOf(',', pos + 1));
			if (!sTmp.equals("-")) {
				detailschedule.add(sTmp);
				timeRow.add(k);
			}

		}
		stations = bundle.getStringArray("stations");
		headTitle = (TextView) findViewById(R.id.DTextViewHead);
		headTitle.setText(bundle.getString("myLocation"));
		l1.setAdapter(new EfficientAdapter(this));
	}

	private class EfficientAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);

		}

		public int getCount() {
			return detailschedule.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.detaillistcontent,
						null);
				holder = new ViewHolder();
				holder.text = (TextView) convertView
						.findViewById(R.id.DTextView01);
				holder.text2 = (TextView) convertView
						.findViewById(R.id.DTextView02);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.text.setText(stations[timeRow.get(position)]);
			holder.text2.setText(detailschedule.get(position));
			convertView.setBackgroundColor((position & 1) == 1 ? 0xff181818
					: 0xff141414);
			return convertView;
		}

		class ViewHolder {
			TextView text;
			TextView text2;
		}
	}

}